import { ElMessage, ElMessageBox } from "element-plus/es";
import api from "@/utils";
import DataOption from "@/components/table/simple/DataOption";

export default function BtnMethods() {
  // 获取数据
  const { data, getRecord } = DataOption();
  // 删除表单数据操作
  const clickDeleteBtn = (
    currentSelects: Array<Record<string, string>>,
    deleteUrl: string
  ) => {
    const selectMultipleId = currentSelects.map(
      (item: Record<string, unknown>) => item.id
    );
    ElMessageBox.confirm(
      '确认删除 "选中的 ' + selectMultipleId.length + ' 条" 记录吗？',
      "Warning",
      {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }
    )
      .then(() => {
        api.http
          .postJson(deleteUrl, selectMultipleId)
          .then(() => {
            ElMessage.success("删除成功!");
            getRecord().then(() => {
              return data.value;
            });
          })
          .catch((err) => {
            console.log(err);
          });
      })
      .catch((cancel) => {
        console.log(cancel);
      });
  };
  return { clickDeleteBtn };
}
